package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.mapper.GoodsMapper;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.service.IGoodsService;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-23
 */
@Service
public class GoodsServiceImpl implements IGoodsService 
{
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询商品
     * 
     * @param goodID 商品主键
     * @return 商品
     */
    @Override
    public Goods selectGoodsByGoodID(Long goodID)
    {
        return goodsMapper.selectGoodsByGoodID(goodID);
    }

    @Override
    public List<Goods> selectMinNum() {
        return goodsMapper.selectMinNum();
    }

    /**
     * 查询商品列表
     * 
     * @param goods 商品
     * @return 商品
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods)
    {
        return goodsMapper.selectGoodsList(goods);
    }

    /**
     * 新增商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoods(Goods goods)
    {
        int rows = goodsMapper.insertGoods(goods);
        insertType(goods);
        return rows;
    }

    /**
     * 修改商品
     * 
     * @param goods 商品
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoods(Goods goods)
    {
        return goodsMapper.updateGoods(goods);
    }

    /**
     * 批量删除商品
     * 
     * @param goodIDs 需要删除的商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoodsByGoodIDs(Long[] goodIDs)
    {
        goodsMapper.deleteTypeByTypeIDs(goodIDs);
        return goodsMapper.deleteGoodsByGoodIDs(goodIDs);
    }

    /**
     * 删除商品信息
     * 
     * @param goodID 商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoodsByGoodID(Long goodID)
    {
        goodsMapper.deleteTypeByTypeID(goodID);
        return goodsMapper.deleteGoodsByGoodID(goodID);
    }

    /**
     * 新增分类名信息
     * 
     * @param goods 商品对象
     */
    public void insertType(Goods goods)
    {
        List<Type> typeList = goods.getTypeList();
        Long goodID = goods.getGoodID();
        if (StringUtils.isNotNull(typeList))
        {
            List<Type> list = new ArrayList<Type>();
            for (Type type : typeList)
            {
                type.setTypeID(goodID);
                list.add(type);
            }
            if (list.size() > 0)
            {
                goodsMapper.batchType(list);
            }
        }
    }
}
